Skip to content

a write snapshots only what it changed; a clean state resolves its saved epoch once - #70

Open
trissim wants to merge 1 commit into
mainfrom
snapshot-walks-captured-scopes
Open

trissim wants to merge 1 commit into
mainfrom
snapshot-walks-captured-scopes

Conversation

@trissim

@trissim trissim commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Summary

This removes two objectstate costs that grew with every registered state: one paid on every write, one on every state opened.

1. A write's snapshot walked every state

_record_snapshot_internal copies only the changed scopes. To find which clean scopes share an object with them, though, it deep-copied every registered state's values on every write. Most of a write's snapshot time went there (in Toad's Qt client at phone speed, object_state_registry.py:1269 was the largest line under update_parameter).

  • SnapshotReach. Each scope now keeps the objects its values reached when it was last captured, together with that capture. It holds those objects, so their ids can't be reused by other objects while the entry lives. Without that, a freed metadata copy's id was taken by a new object and made two independent scopes look shared.
  • Only captured scopes are walked. A scope left clean keeps its capture in the next snapshot and reaches the same objects, so it isn't walked again.
  • Sharing still pulls a clean scope in. A clean scope that shares an object with a captured one is captured with it, transitively, as before. The test_alias_graph_is_detached_and_restored_across_scopes_and_saved_live test covers this.
  • Time travel clears what scopes reached, since restored scopes hold copies. clear() does too.

2. A clean state resolved every field three times

ObjectState.__init__ resolved every field for the live epoch, then again for the saved epoch, and mark_saved resolved the saved epoch once more. Every view Toad opens over saved settings takes this path.

_saved_resolved_snapshot changes that. While a state and every registered ancestor hold exactly their saved parameters (none is is_raw_dirty), both epochs read the same values. In that case the saved epoch is the live snapshot, copied the way saved parameters are, so a live value changed in place still shows as dirty. Otherwise it is resolved on its own, as before.

Why a write re-derives every view (not changed here)

Invalidation was already scoped by scope, type and field. In Toad, every view's configuration is a lazy copy of the whole settings tree that inherits every field it doesn't set. So a global write really does change every open view's resolved value, and each view is a true dependent.

What made a write expensive was work proportional to everything registered, not to the dependents:

  • this whole-registry walk;
  • per-instance annotation resolution under validation, fixed in python-introspect's companion PR.

Measured in Toad's Qt client

  • Sidebar drag release: #2107's qt_drive drag_speed, at phone speed (45% of one core), 3 runs per tree, median. The release carries one settings write.
    • Installed libraries: 420 ms (405, 668, 420).
    • This PR with python-introspect's: 197 ms (222, 197, 170).
  • A view's settings state, built as ObjectConfiguration builds it (unthrottled, median of 5):
    • Installed: 10.5 ms.
    • python-introspect's PR: 4.6 ms.
    • Plus this PR: 3.5 ms, from one resolution instead of three.

Tests

  • 411 passed (main: 409).
  • New tests:
    • test_a_snapshot_walks_only_the_scopes_it_captures:
      • Independent clean scopes keep their capture and reach, unwalked.
      • A scope sharing a list with the edited one is recaptured.
      • Time travel clears the reach.
    • test_a_state_opened_under_saved_ancestors_resolves_its_saved_epoch_as_its_live_one:
      • The saved epoch equals the live one, as a copy.
      • An ancestor with unsaved values gives the saved epoch its own resolution.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3

…ved epoch once

Two costs that grew with every registered state, measured in Toad's Qt client:

- record_snapshot found which clean scopes share an object with a changed one by
  deep-copying every registered state's values on every write, though it copies
  only the changed ones. Each scope now keeps the objects its values reached when
  it was last captured (SnapshotReach, which holds them so their ids stay theirs).
  A scope left clean keeps that capture in the next snapshot and reaches the same
  objects, so only the scopes being captured are walked. A clean scope that shares
  an object with one of them is still captured with it, and time travel forgets
  what scopes reached, since restored scopes hold copies.

- A state opened or saved under ancestors holding exactly their saved parameters
  resolved every field twice in __init__ (live, then saved) and again in
  mark_saved. While this state and every registered ancestor are not raw-dirty,
  both epochs read the same values, so the saved epoch is the live snapshot,
  copied as the saved parameters are (a live value changed in place stays dirty).
  Otherwise it is resolved on its own, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant